package com.foyekoo.project001.config;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.foyekoo.project001.App;
import com.foyekoo.project001.utils.JsonUtil;

/**
 * ${huneng} on 2018/7/31 11:02
 */

public final class SpUtil {


    public static final String KEY_OF_CLIENT = "com.android.demo.KEY_OF_CLIENT";

    private static SharedPreferences preferences;


    private static SpUtil instance;

    public static SpUtil getInstance(){
        if (instance == null) {
            instance = new SpUtil();
            preferences = PreferenceManager.getDefaultSharedPreferences(App.getInstance());
        }
        return instance;
    }

    public LocalClient getLocalClient(){
        String userJson = preferences.getString(KEY_OF_CLIENT , null);
        if (userJson==null) {
            LocalClient client = new LocalClient();
            saveCurrentClient(client);
            return client;
        }
        return JsonUtil.GsonToBean(userJson,LocalClient.class);
    }

    public void saveCurrentClient(LocalClient client) {
        if (client == null) return;
        preferences.edit().putString(KEY_OF_CLIENT,JsonUtil.GsonString(client)).commit();
    }
}
